package com.yun.platform.iot.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yun.platform.iot.dto.ProjectDto;
import com.yun.platform.iot.entity.ProFloor;
import com.yun.platform.iot.entity.ProFloorArea;
import com.yun.platform.iot.entity.Project;
import com.yun.platform.iot.mapper.ProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目表 service 
 *  @author: Hanson  2022-11-24
 */
@Service
public class ProjectService extends ServiceImpl<ProjectMapper, Project>{

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProFloorService proFloorService;
    @Autowired
    private ProFloorAreaService proFloorAreaService;

    public ProjectDto listProFloorArea(Integer proId, Integer floorId) {
        Project project = projectMapper.selectById(proId);
        ProjectDto dto = new ProjectDto();
        BeanUtils.copyProperties(project,dto);
        dto.setProFloorList(proFloorService.lambdaQuery().eq(ProFloor::getProId,proId)
                        .eq((floorId!=null), ProFloor::getId, floorId)
                .list());

        dto.setProFloorAreaList(proFloorAreaService.lambdaQuery().eq(ProFloorArea::getProId, proId)
                        .eq((floorId!=null),ProFloorArea::getFloorId,floorId)
                .list());
        return dto;
    }

    /**
     * 关联查询测试 join-plus 感觉不是很适用 省不了多少代码。
     * @param proId
     * @param floorId
     * @return
     * 通过别名查询：.selectAs(" 要查询的字段(方法的形式填入) " , " 字段别名(方法的形式填入) ")
     * 查询：.select("要查询的字段(方法的形式填入)")
     * 联表：.leftJoin("联合表的类" , "绑定的key" , "绑定的key")
     */
    public List<ProjectDto> listProFloorAreats(Integer proId, Integer floorId) {
        List<ProjectDto> projectDtos = projectMapper.selectJoinList(ProjectDto.class, new MPJLambdaWrapper<Project>()
                .selectAs(Project::getId, ProjectDto::getId)
                .select(Project::getProjectName)
                .innerJoin(ProFloor.class, ProFloor::getId, ProFloor::getProId)
                .eq(ProFloor::getProId, proId)
                .eq((floorId != null), ProFloor::getId, floorId));
        return projectDtos;

    }
}